package com.xyzboom.web.services;


import com.xyzboom.beans.Login;
import com.xyzboom.database.dao.UserDAO;
import com.xyzboom.database.entity.User;

import java.sql.SQLException;

public class LoginService {
    public static LoginResult login(Login login) throws SQLException {
        String username = login.getUsername();
        String password = login.getPassword();
        String school = login.getSchool();
        String department = login.getDepartment();
        if (!UserDAO.userExists(username, school, department)) {
            return new LoginResult(LoginType.NO_USER, null);
        }
        User user = UserDAO.getUser(username, school, department);
        assert user != null;
        if (user.getPassword().equals(password) && user.getSchool().equals(school)
                && user.getDepartment().equals(department)) {
            return new LoginResult(LoginType.SUCCESS, user);
        } else {
            return new LoginResult(LoginType.PASSWORD_ERROR, user);
        }
    }

    public static class LoginResult {
        LoginType loginType;
        User user;

        public LoginType getLoginType() {
            return loginType;
        }

        public User getUser() {
            return user;
        }

        public LoginResult(LoginType loginType, User user) {
            this.loginType = loginType;
            this.user = user;
        }
    }

    public enum LoginType {
        SUCCESS,
        NO_USER,
        PASSWORD_ERROR,
    }
}
